python字符串传参 | 您所在的位置:网站首页 › python argparse传列表 › python字符串传参 |
要获得您正在寻找的内容,诀窍是使用parse_known_args()而不是parse_args(): #!/bin/env python import argparse parser = argparse.ArgumentParser() parser.add_argument('-a', action="store_true") parser.add_argument('-b', action="store_true") opts = parser.parse_known_args() # Print info about flags if opts[0].a: print('You set the option "-a"') if opts[0].b: print('You set the option "-b"') # Collect remainder (opts[1] is a list (possibly empty) of all remaining args) if opts[1]: print('You passed the strings %s' % opts[1]) 编辑: 以上代码显示以下帮助信息: ./clargs.py -h usage: clargs_old.py [-h] [-a] [-b] optional arguments: -h, --help show this help message and exit -a -b 如果你想告诉用户可选的任意参数,我能想到的唯一解决方案是继承Argum |
CopyRight 2018-2019 实验室设备网 版权所有 |